#!/bin/bash

# This is a script that attempts to *approximately* exhaustively run the test
# suite for regex-automata. The main reason for why 'cargo test' isn't enough
# is because of crate features. regex-automata has a ton of them. This script
# tests many of those feature combinations (although not all) to try to get
# decent coverage in a finite amount of time.

set -e

# cd to the directory containing this crate's Cargo.toml so that we don't need
# to pass --manifest-path to every `cargo` command.
cd "$(dirname "$0")"

echo "===== ALL FEATURES TEST ==="
cargo test --all-features

# Man I don't *want* to have this many crate features, but... I really want
# folks to be able to slim the crate down to just the things they want. But
# the main downside is that I just can't feasibly test every combination of
# features because there are too many of them. Sad, but I'm not sure if there
# is a better alternative.
features=(
  ""
  "unicode-word-boundary"
  "unicode-word-boundary,syntax,unicode-perl"
  "unicode-word-boundary,syntax,dfa-build"
  "nfa"
  "dfa"
  "hybrid"
  "nfa,dfa"
  "nfa,hybrid"
  "dfa,hybrid"
  "dfa-onepass"
  "nfa-pikevm"
  "nfa-backtrack"
  "std"
  "alloc"
  "syntax"
  "syntax,nfa-pikevm"
  "syntax,hybrid"
  "perf-literal-substring"
  "perf-literal-multisubstring"
  "meta"
  "meta,nfa-backtrack"
  "meta,hybrid"
  "meta,dfa-build"
  "meta,dfa-onepass"
  "meta,nfa,dfa,hybrid,nfa-backtrack"
  "meta,nfa,dfa,hybrid,nfa-backtrack,perf-literal-substring"
  "meta,nfa,dfa,hybrid,nfa-backtrack,perf-literal-multisubstring"
)
for f in "${features[@]}"; do
  echo "===== LIB FEATURES: $f ==="
  # It's actually important to do a standard 'cargo build' in addition to a
  # 'cargo test'. In particular, in the latter case, the dev-dependencies may
  # wind up enabling features in dependencies (like memchr) that make it look
  # like everything is well, but actually isn't. For example, the 'regex-test'
  # dev-dependency uses 'bstr' and enables its 'std' feature, which in turn
  # unconditionally enables 'memchr's 'std' feature. Since we're specifically
  # looking to test that certain feature combinations work as expected, this
  # can lead to things testing okay, but would actually fail to build. Yikes.
  cargo build --no-default-features --lib --features "$f"
  cargo test --no-default-features --lib --features "$f"
done

# We can also run the integration test suite on stripped down features too.
# But the test suite doesn't do well with things like 'std' and 'unicode'
# disabled, so we always enable them.
features=(
  "std,unicode,syntax,nfa-pikevm"
  "std,unicode,syntax,nfa-backtrack"
  "std,unicode,syntax,hybrid"
  "std,unicode,syntax,dfa-onepass"
  "std,unicode,syntax,dfa-search"
  "std,unicode,syntax,dfa-build"
  "std,unicode,meta"
  # This one is a little tricky because it causes the backtracker to get used
  # in more instances and results in failing tests for the 'earliest' tests.
  # The actual results are semantically consistent with the API guarantee
  # (the backtracker tends to report greater offsets because it isn't an FSM),
  # but our tests are less flexible than the API guarantee and demand offsets
  # reported by FSM regex engines. (Which is... all of them except for the
  # backtracker.)
  # "std,unicode,meta,nfa-backtrack"
  "std,unicode,meta,hybrid"
  "std,unicode,meta,dfa-onepass"
  "std,unicode,meta,dfa-build"
  "std,unicode,meta,nfa,dfa-onepass,hybrid"
)
for f in "${features[@]}"; do
  echo "===== INTEGRATION FEATURES: $f ==="
  cargo build --no-default-features --lib --features "$f"
  cargo test --no-default-features --test integration --features "$f"
done
